﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

namespace MainProgram.ModelReport
{
    public partial class FormReport : Form
    {

        SqlConnection myConnection = new SqlConnection();
        SqlCommand myCommand = new SqlCommand();

        string myType;

        public FormReport()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (myConnection.State != ConnectionState.Open)
            {
                string strConnection = @"Data Source=.\SQLEXPRESS;Initial Catalog=Monitor;User ID=root;Password=123";
                myConnection.ConnectionString = strConnection;
                myConnection.Open();
                myCommand.Connection = myConnection;
            }
        }

        private void FormReport_FormClosing(object sender, FormClosingEventArgs e)
        {
            myConnection.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (myConnection.State == ConnectionState.Open)
            {
                string strSQL = "SELECT DateTime,DeviceID,Value " +
                    "FROM MonitorData " +
                    "WHERE DeviceID=" + comboBox1.Text;
                if (comboBox1.Text == "")
                {
                    MessageBox.Show("请输入设备号");
                    return;
                }
                if (textBox1.Text != "")
                {
                    strSQL += " AND DateTime>='" + textBox1.Text + "'";
                }
                if (textBox2.Text != "")
                {
                    strSQL += " AND DateTime<='" + textBox2.Text + "'";
                }



                //get device type
                myCommand.CommandText = "SELECT Type FROM DeviceInfo WHERE DeviceID=" + Convert.ToInt32(comboBox1.Text);
                SqlDataReader myDataReader = myCommand.ExecuteReader();
                myDataReader.Read();
                myType = myDataReader["Type"].ToString();
                myDataReader.Close();



                DataSetReport ds1 = new DataSetReport();
                SqlDataAdapter da = new SqlDataAdapter();
                da = new SqlDataAdapter(strSQL, myConnection);
                da.Fill(ds1, "DataTableDevice");


                dataGridView1.DataSource = ds1;
                dataGridView1.DataMember = ds1.DataTableDevice.ToString();


#if DEBUG
                string reportPath = System.IO.Directory.GetCurrentDirectory().Replace("bin\\Debug", "ModelReport\\CrystalReportDevice.rpt");
#else
                string reportPath = System.IO.Directory.GetCurrentDirectory().Replace("bin\\Release", "ModelReport\\CrystalReportDevice.rpt");
#endif

                ReportDocument myReport = new ReportDocument();
                myReport.Load(reportPath);
                myReport.SetDataSource(ds1.DataTableDevice.Copy());
                crystalReportViewer1.ReportSource = myReport;
                crystalReportViewer1.RefreshReport();
            }
            else
            {
                MessageBox.Show("请先连接数据库");
            }
        }
    }
}
